package com.gitee.wsl.transform.validator.bean

import kotlin.reflect.full.declaredMemberProperties

/**
 * Represents a validation constraint
 *
 * @property name specifies the name of the constraint, generally it will be the name of the class
 * @property messageBundle specifies the base name of the default message properties file
 * @property messageKey specifies the name of the key in the message properties file
 * @property messageParams specifies the parameters to replace in the message
 *
 * @author Rodolpho S. Couto
 * @since 0.1.0
 */
interface Constraint {

    val name: String
        get() = this::class.simpleName.toString()

    val messageKey: String
        get() = "${this::class.qualifiedName}.message"

    val messageParams: Map<String, *>
        get() = this::class.declaredMemberProperties
            .asSequence()
            .filter {
                Constraint::class.declaredMemberProperties
                    .none { p -> p.name == it.name }
            }
            //.map { it.name to it.get(this) }
            .map { it.name to it}
            .toMap()

}

class ThrowAbleConstraint(val throwable: Throwable):Constraint{
    override fun toString(): String {
        return throwable.toString()
    }
}